//
//  WKSubmitBookVC.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/17.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKSubmitBookVC.h"
#import "WKBookSelectPhotoCell.h"
#import "WKSubmitBookCell.h"

@interface WKSubmitBookVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSArray <UIImage *>*imgs;
@end

@implementation WKSubmitBookVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
}

- (void)initViews{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的日记";
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight-NavBarHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [_tableview registerClass:[WKBookSelectPhotoCell class] forCellReuseIdentifier:NSStringFromClass([WKBookSelectPhotoCell class])];
    [_tableview registerClass:[WKSubmitBookCell class] forCellReuseIdentifier:@"BOOKTITLEVIEW"];
    [_tableview registerClass:[WKSubmitBookCell class] forCellReuseIdentifier:@"BOOKTAGVIEW"];
    [_tableview registerClass:[WKSubmitBookCell class] forCellReuseIdentifier:@"BOOKCONTENTVIEW"];
    [_tableview registerClass:[WKSubmitBookCell class] forCellReuseIdentifier:@"BOOKVISIBLEVIEW"];
    
    [self.view addSubview:self.tableview];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
    WKBookSelectPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WKBookSelectPhotoCell class]) forIndexPath:indexPath];
    @weakify(self);
    cell.selectImgBlock = ^(NSArray * _Nonnull imgs) {
        @strongify(self);
        self.imgs = imgs;
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        [tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:(UITableViewRowAnimationFade)];
    };
    [cell displayImgWithImgArr:self.imgs];
    return cell;
    }
    NSArray *identifierArr = @[@"BOOKTITLEVIEW",@"BOOKTAGVIEW",@"BOOKCONTENTVIEW",@"BOOKVISIBLEVIEW"];
    NSString *ID = identifierArr[indexPath.row-1];
    WKSubmitBookCell *bookCell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    return bookCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CGFloat height = 0;
        CGFloat itemHeigth = (ScreenWidth - 24 -10)/3;
        if (self.imgs.count>=3 && self.imgs.count<6) {
           
            height = 40 + itemHeigth*2+10+5;
        }else if(self.imgs.count>=6){
             height = 40 + itemHeigth*3+10+5*2;
        }else{
            height = 40 + itemHeigth + 10;
        }
        
        return height;
    }
    
    if (indexPath.row==3) {
        return 330;
    }
    
    return 44;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
